﻿namespace Filters.Demo.Core
{
    using System;
    using System.Globalization;
    using System.IO;

    /// <summary>
    /// FilterApplierEngine class
    /// </summary>
    public class FilterApplierEngine : IFilterApplierEngine
    {
        private readonly IImageFilter imageFilter;

        public FilterApplierEngine(IImageFilter imageFilter)
        {
            this.imageFilter = imageFilter;
        }

        public void ApplyFilters(string filterFile)
        {
            if (!File.Exists(filterFile))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The filters file {0} does not exist.", filterFile), "filterFile");
            }

            var configuration = new ConfigurationBuilder(filterFile);
            var filtersInfo = configuration.ReadFiltersConfiguration();

            this.imageFilter.Reload();

            foreach (var filterInfo in filtersInfo)
            {
                this.imageFilter.ApplyFilter(filterInfo.Filter, filterInfo.FilterData);
            }
        }

        public void Save(string outputPath)
        {
            if (!Directory.Exists(outputPath))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The output directory {0} does not exist.", outputPath), "outputPath");
            }

            this.imageFilter.Save(outputPath);
        }
    }
}
